import BaseItemModel from "../../../cfw/mvc/BaseItemModel";
import ItemController from "../../item/ItemController";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import { ItemState } from "../../../cfw/tools/Define";
import { ItemID } from "../../../public/Config";
import StringHelper from "../../../cfw/tools/StringHelper";
import BaseItemView from "../../../cfw/mvc/BaseItemView";
import SnowToyModel from "../model/SnowToyModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SnowToyView extends BaseItemView {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Sprite)
    ad: cc.Sprite = null;

    @property(cc.Sprite)
    coin: cc.Sprite = null;

    @property(cc.Label)
    coinLabel: cc.Label = null;


    protected model: SnowToyModel
    protected controller: ItemController;

    needBuy() {
        return this.model.needBuy();
    }

    updateState() {
        if (!cc.isValid(this.node) || !this.model) {
            return;
        }
        let cost: number[] = this.model.getCostItem()
        if (cost && cost.length >= 2 && this.model.getState() == ItemState.NOT_GET) {

            switch (cost[0]) {
                case ItemID.AD:
                    this.coin.node.active = false
                    this.ad.node.active = true;
                    this.ad.node.scale = this.model.getScale()
                    break;
                case ItemID.COIN:
                    this.ad.node.active = false;
                    this.coin.node.active = true
                    this.coin.node.scale = this.model.getScale()
                    this.coinLabel.string = StringHelper.getShortStr(cost[1])
                    break;
            }
        } else {
            this.ad.node.active = false;
            this.coin.node.active = false;
        }
    }

    // setState(s) {
    //     this.model.setState(s)
    //     this.updateState()
    // }

    // onDestroy() {
    //     this.model.off(BaseItemModel.UPDATE_STATE, this.updateState, this)
    // }

    addListener() {
        this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.updateState, this)
    }
    refresh() {
        ModuleManager.getLoader().loadRes(this.model.getIcon(), ResType.SpriteFrame, (err, res: ResItem) => {
            if (err || !cc.isValid(this.node)) {
                return;
            }
            this.icon.spriteFrame = res.getRes()
            this.icon.node.scale = this.model.getScale()
        })
        this.updateState()

    }
}
